from django.shortcuts import render,redirect
from django.views.generic import FormView
from comments.forms import CommentsForm
from django.http import HttpResponse
from comments.models import Comments
from django.urls import reverse_lazy



# Create your views here.

class CommentView(FormView):
    form_class = CommentsForm
    template_name = 'single.html'

    def form_valid(self, form):
        user = self.request.user
        print(user)
        if not user.pk:
            return HttpResponse('fail')
        Comments.objects.create(user=user, **form.cleaned_data)
        url = self.request.META['HTTP_REFERER']
        return redirect(url)

    def form_invalid(self, form):
        url = self.request.META['HTTP_REFERER']
        return redirect(url)
